<?php
class UsermanageController extends Controller {
	public function index() {
		$smaryt = $this->getSmarty ();
		
		//如果不是post方式的提交，直接转向
// 		if(!CommonBase::isPost()){
// 			$this->smarty->display("usermanage.tpl");
// 			return;
// 		}
		
		
		$user = $_SESSION ['user'];
		
		if(empty($user->user_photo)){
			$user->user_photo = "user-iocn.gif";
		} 
// 		require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
// 		$userService = new UsersService ( $this->getDB () );
		
// 		$user = $userService->getUserInfoByID ( $suser->userid );
 
		$this->smarty->display ( "usermanage.tpl" );
	}
	
	public function getMsg() {
		$smaryt = $this->getSmarty ();
		$user = $_SESSION ['user'];
		echo  json_encode ($user);
	}
	public function modify() {
		$smaryt = $this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
		$userService = new UsersService ( $this->getDB () );
		$password = $_POST ["password"];
		$user = $_SESSION ['user'];
		if($password != $user->password){
			$password = MD5($password);
		}
		$input_data = array (
				'username' => $_POST ["username"],
				'email' => $_POST ["email"],
				'email' => $_POST ["email"],
				'firstname' => $_POST ["firstname"],
				'lastname' => $_POST ["lastname"],
				'user_photo' => $_POST ["user_photo"],
				'password' =>$password
		);
		$input_condition = array (
				'userid' => $_POST ["userid"]
		);
		
		$user_id = $userService->edit ( $input_data, $input_condition );
		if($user_id >0){
			$newuser = $userService->getUserInfoByID($_POST ["userid"]);
			$_SESSION ['user'] =$newuser;
		}
		echo $user_id;
	}
}